﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GSM.BusinessLayer.DataLayer;
using GSM.BusinessLayer;
using DevExpress.XtraEditors;
using CRM_SMS_MARKETING.DAL;
using DevExpress.XtraGrid;
using CRM_SMS_MARKETING.EL;
using System.Windows.Forms;
using MD5;

namespace CRM_SMS_MARKETING.BL
{
    class BLEvent
    {

        EventSql eventsql;
        PromotionSql promotionsql;
        CustomerSql customersql;
        Event_CustomerSql ECsql;
        public BLEvent()
        {
            eventsql = new EventSql();
            customersql = new CustomerSql();
            promotionsql = new PromotionSql();
            ECsql = new Event_CustomerSql();
        }
        public List<Event_Customer> GetEventCustomerListByEventID(int EventID)
        {
            List<Event_Customer> ListEventCustomer = new List<Event_Customer>();
            ListEventCustomer=ECsql.SelectByField("EventID", EventID);
            return ListEventCustomer;

        }
        public string ParseSMS(string smsFormat, string customername, string companyName, string companyAddress, string dateSubject, string dateStart, string dateEnd, string voucher, string gift)
        {
            char[] smsArray = new char[1100];
            smsArray = smsFormat.ToCharArray();

            string result = "";
            int n = smsArray.Length;
            int i = 0;
            while (i < n)
            {
                if (smsArray[i].Equals('{'))
                {
                    #region CustomerName, Company
                    if (smsArray[i + 1] == 'C')
                    {
                        if (smsArray[i + 8] == 'r')
                        {
                            //CustomerName
                            result = result + customername;
                            i = i + 13;
                        }
                        else
                            if (smsArray[i + 8] == 'N')
                            {
                                //CompanyName
                                result = result + companyName;
                                i = i + 12;
                            }
                            else
                                if (smsArray[i + 8] == 'A')
                                {
                                    //CompanyAddress
                                    result = result + companyAddress;
                                    i = i + 15;
                                }
                    }
                    #endregion
                    #region Date
                    else
                        if (smsArray[i + 1] == 'D')
                        {
                            if (smsArray[i + 6] == 'u')
                            {
                                //DateSubject
                                result = result + dateSubject;
                                i = i + 12;
                            }
                            else
                                if (smsArray[i + 6] == 't')
                                {
                                    //DateStart
                                    result = result + dateStart;
                                    i = i + 10;
                                }
                                else
                                    if (smsArray[i + 6] == 'n')
                                    {
                                        //DateEnd
                                        result = result + dateEnd;
                                        i = i + 9;
                                    }
                        }
                    #endregion
                        #region Gift Voucher
                        else
                            if (smsArray[i + 1] == 'G')
                            {
                                //Gift Subject
                                result = result + gift;
                                i = i + 5;
                            }
                            else
                                if (smsArray[i + 1] == 'V')
                                {
                                    //Voucher Subject
                                    result = result + voucher;
                                    i = i + 8;
                                }

                        #endregion
                }
                else
                {
                    result = result + smsArray[i];
                }

                i++;

            }

            return result;
        }
        public void CreateSMSForBirthday(Event even, string VoucherSubject, string GiftSubject, List<GSM.BusinessLayer.Customer> ListCustomer, int MessageID)
        {

            int n = ListCustomer.Count;
            Customer_MessageSql cmsql = new Customer_MessageSql();
            CompanySql companysql = new CompanySql();
            CompanyKeys keys = new CompanyKeys(1);
            Company company = new Company();
            company = companysql.SelectByPrimaryKey(keys);
            string companyName = company.CompanyName;
            string companyAddress = company.CompanyAddress;
            List<Event_Customer> ListEventCustomer = new List<Event_Customer>();
            ListEventCustomer = GetEventCustomerListByEventID(1);

            for (int i = 0; i < n; i++)
            {
                if (ListEventCustomer[i].EventSMSStatus == false)
                {
                    ListEventCustomer[i].EventSMSStatus=true;
                    ECsql.Update(ListEventCustomer[i]);
                    Customer_Message CM = new Customer_Message();
                    CM.CustomerID = ListCustomer[i].CustomerID;
                    CM.MessageMarketID = MessageID;
                    CM.Date = ListCustomer[i].Birthday;
                    CM.Deleted = false;
                    CM.Type = 2;
                    CM.Phone = ListCustomer[i].CellPhone;
                    CM.Content = ParseSMS(even.Content, ListCustomer[i].FirstName, companyName, companyAddress, even.Subject, even.BeginDate.ToString(), even.FinishDate.ToString(), VoucherSubject, GiftSubject);
                    CM.EventID = even.EventID;
                    cmsql.Insert(CM);
                }
            }
        }
        public void CreateSMSForEvent(Event even, string VoucherSubject, string GiftSubject, List<GSM.BusinessLayer.Customer> ListCustomer)
        {

            int n = ListCustomer.Count;
            Customer_MessageSql cmsql = new Customer_MessageSql();
            CompanySql companysql = new CompanySql();
            CompanyKeys keys = new CompanyKeys(1);
            Company company = new Company();
            company = companysql.SelectByPrimaryKey(keys);
            string companyName = company.CompanyName;
            string companyAddress = company.CompanyAddress;

            for (int i = 0; i < n; i++)
            {
                Customer_Message CM = new Customer_Message();
                CM.CustomerID = ListCustomer[i].CustomerID;
                CM.Date = even.BeginDate;
                CM.Deleted = false;
                CM.Type = 2;
                CM.Phone = ListCustomer[i].CellPhone;
                CM.Content = ParseSMS(even.Content, ListCustomer[i].FirstName, companyName, companyAddress, even.Subject, even.BeginDate.ToString(), even.FinishDate.ToString(), VoucherSubject, GiftSubject);
                CM.EventID = even.EventID;
                cmsql.Insert(CM);
            }
        }
        public List<Event> RefreshGrid(List<Event> ListEvent, GridControl gridControlEvent)
        {

            ListEvent = eventsql.SelectByField("Deleted", 0);
            gridControlEvent.DataSource = ListEvent;
            return ListEvent;
        }
        public void CreateListVoucherAndGift(List<SmallPromotion> voucher, List<SmallPromotion> gift, List<ELNewPromotion> ListVoucher, List<ELNewPromotion> ListGift)
        {


            int n = voucher.Count;
            for (int i = 0; i < n; i++)
            {
                ELNewPromotion npro = new ELNewPromotion();
                npro.ID = voucher[i].PromotionID.ToString();
                npro.PromotionSubject = voucher[i].PromotionSubject.ToString();
                ListVoucher.Add(npro);
            }
            ListVoucher[0].ID = "";
            int m = gift.Count;
            for (int i = 0; i < m; i++)
            {
                ELNewPromotion npro = new ELNewPromotion();
                npro.ID = gift[i].PromotionID.ToString();
                npro.PromotionSubject = gift[i].PromotionSubject.ToString();
                ListGift.Add(npro);
            }
            ListGift[0].ID = "";
        }
        public Promotion GetPromotionByID(string ID)
        {
            Promotion pro = new Promotion();
            try
            {
                if (string.IsNullOrEmpty(ID) == false)
                {
                    PromotionKeys keys = new PromotionKeys(Int32.Parse(ID));

                    pro = promotionsql.SelectByPrimaryKey(keys);
                }
                else
                {
                    pro.PromotionContent = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return pro;
        }
        public Event GetEventByID(int ID)
        {

            Event even = new Event();
            try
            {


                EventKeys keys = new EventKeys(ID);

                even = eventsql.SelectByPrimaryKey(keys);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return even;
        }
        public void EvenCustomerDeleteByField(string fieldname, object value)
        {
            Event_CustomerSql ecsql = new Event_CustomerSql();
            ecsql.DeleteByField(fieldname, value);
        }
        public void EventCustomerInsertList(int EventID, List<GSM.BusinessLayer.Customer> list)
        {
            foreach (GSM.BusinessLayer.Customer cus in list)
            {
                EventCustomerInsert(EventID, cus.CustomerID);
            }
        }
        public List<GSM.BusinessLayer.Customer> GetListCustomerOfEvent(int eventID)
        {
            List<GSM.BusinessLayer.Customer> ListCustomer = new List<Customer>();
            Event_CustomerSql ecsql = new Event_CustomerSql();
            List<Event_Customer> ListEventCustomer = new List<Event_Customer>();
            ListEventCustomer = ecsql.SelectByField("EventID", eventID);

            int n = ListEventCustomer.Count;
            for (int i = 0; i < n; i++)
            {
                GSM.BusinessLayer.Customer cus = new GSM.BusinessLayer.Customer();
                CustomerKeys keys = new CustomerKeys(ListEventCustomer[i].CustomerID);
                CustomerSql customersql = new CustomerSql();
                cus = customersql.SelectByPrimaryKey(keys);
                ListCustomer.Add(cus);
            }
            return ListCustomer;
        }
        public void EventCustomerInsert(int eventID, int customerID)
        {
            Event_Customer ec = new Event_Customer();
            ec.CustomerID = customerID;
            ec.EventID = eventID;
            ec.Code = GetCode();
            ec.Status = false;
            Event_CustomerSql ecsql = new Event_CustomerSql();
            try
            {
                ecsql.Insert(ec);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public string GetCode()
        {
            MyMD5 md5 = new MyMD5();
            return md5.HashMessage(DateTime.Now.Millisecond.ToString());
        }
        public List<GSM.BusinessLayer.Customer> GetListCustomerWithChecked(List<GSM.BusinessLayer.Customer> ListCustomer)
        {
            List<GSM.BusinessLayer.Customer> ListAll = new List<Customer>();
            ListAll = customersql.SelectByField("Deleted", 0);
            int n = ListAll.Count;
            int m = ListCustomer.Count;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (ListAll[i].CustomerID == ListCustomer[j].CustomerID)
                    {
                        ListAll[i].Deleted = true;
                    }
                }
            }
            return ListAll;
        }

    }
}

